package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.Comment;
import com.cskaoyan.bean.pojo.CommentExample;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.wx.auth.vo.UserInfoVo;
import com.cskaoyan.bean.wx.comment.bo.CommentBo;
import com.cskaoyan.bean.wx.comment.vo.CommentDetailVo;
import com.cskaoyan.bean.wx.comment.vo.CommentListVo;
import com.cskaoyan.bean.wx.comment.vo.CommentVo;
import com.cskaoyan.bean.wx.comment.vo.CountVo;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.wx.WxCommentService;
import com.cskaoyan.util.CurrentUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Name : WxCommentServiceImpl.java
 * @Time : 2021/8/17 11:12
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@Service
@Transactional
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public CommentListVo getCommentList(Integer valueId, Byte type, Byte showType, Integer page, Integer size) {
        CommentListVo commentListVo = new CommentListVo();
        PageHelper.startPage(page,size);
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        criteria.andDeletedEqualTo(false);
        if(showType == 1){
            criteria.andHasPictureEqualTo(true);
        }
        Integer currentUserId = CurrentUserUtils.getCurrentUserId(userMapper);
        User user = userMapper.selectByPrimaryKey(currentUserId);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        List<CommentVo> commentVos = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVo commentVo = transfer2CommentVo(comment,user.getNickname(),user.getAvatar());
            commentVos.add(commentVo);
        }
        PageInfo<Comment> pageInfo = new PageInfo<>();
        int total = pageInfo.getPages();
        commentListVo.setData(commentVos);
        commentListVo.setCurrentPage(page);
        commentListVo.setCount(total);
        return commentListVo;
    }

    @Override
    public CommentDetailVo postComment(CommentBo commentBo) {
        Comment comment = transfer2Comment(commentBo);
        Integer currentUserId = CurrentUserUtils.getCurrentUserId(userMapper);
        comment.setUserId(currentUserId);
        commentMapper.insert(comment);
        CommentDetailVo commentDetailVo = transfer2CommentDetailVo(comment);
        return commentDetailVo;
    }

    @Override
    public CountVo getCount(Integer valueId, Byte type) {
        CountVo countVo = new CountVo();
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        criteria.andDeletedEqualTo(false);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        countVo.setAllCount(comments.size());
        criteria.andHasPictureEqualTo(true);
        List<Comment> commentsHasPic = commentMapper.selectByExample(commentExample);
        countVo.setHasPicCount(commentsHasPic.size());
        return countVo;
    }

    private CommentDetailVo transfer2CommentDetailVo(Comment comment) {
        CommentDetailVo commentDetailVo = new CommentDetailVo();
        commentDetailVo.setId(comment.getId());
        commentDetailVo.setType(comment.getType());
        commentDetailVo.setValueId(comment.getValueId());
        commentDetailVo.setContent(comment.getContent());
        commentDetailVo.setStar(comment.getStar());
        commentDetailVo.setHasPicture(comment.getHasPicture());
        commentDetailVo.setPicUrls(comment.getPicUrls());
        commentDetailVo.setAddTime(comment.getAddTime());
        commentDetailVo.setUpdateTime(comment.getUpdateTime());
        commentDetailVo.setDeleted(comment.getDeleted());
        return commentDetailVo;
    }

    private Comment transfer2Comment(CommentBo commentBo) {
        Comment comment = new Comment();
        comment.setType(commentBo.getType());
        comment.setValueId(commentBo.getValueId());
        comment.setContent(commentBo.getContent());
        comment.setStar(commentBo.getStar());
        comment.setHasPicture(commentBo.getHasPicture());
        comment.setPicUrls(commentBo.getPicUrls());
        comment.setAddTime(new Date(System.currentTimeMillis()));
        comment.setUpdateTime(new Date(System.currentTimeMillis()));
        comment.setDeleted(false);
        return comment;
    }

    private CommentVo transfer2CommentVo(Comment comment, String username, String avatar) {
        CommentVo commentVo = new CommentVo();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setNickName(username);
        userInfoVo.setAvatarUrl(avatar);
        commentVo.setUserInfo(userInfoVo);
        commentVo.setAddTime(comment.getAddTime());
        commentVo.setPicList(comment.getPicUrls());
        commentVo.setContent(comment.getContent());
        return commentVo;
    }
}
